package com.gisuni.sddc.platformManager.service;

import org.nutz.log.Log;
import org.nutz.log.Logs;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;

import com.gisuni.sddc.platformManager.dao.LayerInfoDao;
import com.gisuni.sddc.platformManager.model.LayerInfo;
import com.gisuni.sddc.platformManager.model.Response;
import com.gisuni.sddc.platformManager.model.ServerInfo;
import com.gisuni.sddc.util.StringUtil;

import java.util.Date;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

@IocBean
public class LayerInfoService   {

	private Log log = Logs.getLog(LayerInfoService.class);    
    @Inject
    private LayerInfoDao layerInfoDao;
  
 
	public Response queryBaseLayerInfoAll(int from, int to, String name) {
		Response response = new Response();
		response.setSuccess(false);
		String Result=null;
 
		try {
			String result = layerInfoDao.queryBaseServerInfoAll(  from,  to,  name);
			int total = layerInfoDao.queryBaseServerInfoCount( name);
			response.setSuccess(true);
			response.setResult(result);
			response.setTotal(total);
			return response;
		}catch (Exception e) {
			e.printStackTrace();
			log.error(e.getMessage());
			response.setMsg("查询失败");
			return response;
		}
		
	}

  
    public ServerInfo findRoleById(String id) {
      return layerInfoDao.findServerInfoById(id);
    }

    

	public Response addLayerInfo(HttpServletRequest request) throws Exception{
   	     Response res = new Response();
   	     res.setSuccess(false);
		 String layerInfo = request.getParameter("layerInfo");
		 String serverInfoId = request.getParameter("serverInfoId");
		 boolean flag = false ; 
		 if(!StringUtil.isNullOrEmpty(layerInfo)){
			 String[] layerInfos = layerInfo.split("\\|");
			 
			 for(int i= 0 ;i<layerInfos.length;i++){
				 String  id = UUID.randomUUID().toString().replaceAll("-", "");
				 String name = layerInfos[i];
				 int firstIndex = name.lastIndexOf("(");
				 
				 LayerInfo entity = new LayerInfo();
				 entity.setId(id);
				 entity.setLayerName(name.substring(0, firstIndex));
				 int lastIndex = name.lastIndexOf(")");
				 entity.setLayerIndex(Integer.parseInt(name.substring(firstIndex+1, lastIndex)+"" ));
				 entity.setCreateTime(new Date());
				 entity.setServerId(serverInfoId);
				 //TODO entity.setTableName();
				 flag =  layerInfoDao.addLayerInfo(entity);
			 }
			 
			 
		 }else{
			 res.setSuccess(false);
			 res.setMsg("图层信息为空,操作失败!!!");
		 }
//		 LayerInfo layerInfo = new LayerInfo();
//		 layerInfo.setServerId(serverInfoId);
//		 layerInfo.setId(id);
//		 layerInfo.setCreateTime(new Date());
//		 
// 
		 
		 if(flag){
			 res.setSuccess(true);
		 } 
		 
		return res;
	}
	
  
    public Response updateLayerInfo(HttpServletRequest request) {
      	 	 Response res = new Response();
       	 	 res.setSuccess(false);
       	 	 
       	 	 //删除相应的数据
       	 	 //LayerId变了!!!!!!
       	     String roleName = request.getParameter("name");
    		 String id = request.getParameter("id");
    		 String serverName = request.getParameter("status");
    		 String type = request.getParameter("type");
    		 String dynamicsUrl = request.getParameter("description");
    		 String featureUrl = request.getParameter("featureUrl");
    		 String  status = request.getParameter("status");
    		 String  description = request.getParameter("description");
    		 
    		 ServerInfo serverInfo = new ServerInfo();
    		 serverInfo.setServerName(serverName);
    		 serverInfo.setType(type);
    		 serverInfo.setStatus(status);
    		 serverInfo.setDynamicsUrl(dynamicsUrl);
    		 serverInfo.setFeatureUrl(featureUrl); 
    		 serverInfo.setId(id);
    		 serverInfo.setUpdateTime(new Date());
    		 serverInfo.setDescription(description);
    		 //TODO 判断
    		 
//    		 boolean flag  = layerInfoDao.updateServerInfo(serverInfo);
//    		 if(flag){
//    			 res.setSuccess(true);
//    		 } 
    		 
    		return res;
    }
    
    /**
     * 根据serverId 与 index 更新
     * @param request
     * @return
     */
    public Response updateLayerInfoByServerId(HttpServletRequest request) {
 	 	 Response res = new Response();
  	 	 res.setSuccess(false);
 
  	     String tableName = request.getParameter("tableName");
		 String serverInfoId = request.getParameter("serverInfoId");
		 String layerIndex = request.getParameter("layerIndex");
		 
		 String sql = "update SDDCGISPLATFORM.LAYER_INFO set TABLE_NAME='"+tableName+"' where SERVER_ID='"+serverInfoId+"' and LAYER_INDEX="+layerIndex;
		 boolean flag = layerInfoDao.updateBySQL(sql);
		 if(flag){
			 res.setSuccess(true);
		 }
		 
		return res;
    }
    
	  
    public Response deleteLayerInfoById(String id) {
   	 Response res = new Response();
   	 res.setSuccess(false);
   	 try{
     	ServerInfo serverInfo = new ServerInfo();
     	serverInfo.setId(id);
//    	boolean flag  = layerInfoDao.delServerInfo(serverInfo);
//    	 if(flag){
//    		 res.setSuccess(true);
//    	 }
    	 return res;
   	 }catch(Exception e){
   		 e.printStackTrace();
   		 log.error(e);
   		 res.setMsg("操作失败");
   		 return res;
   	 }
    }

}
